package com.swiot.biz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.swiot.common.annotation.Log;
import com.swiot.common.core.controller.BaseController;
import com.swiot.common.core.domain.AjaxResult;
import com.swiot.common.enums.BusinessType;
import com.swiot.common.utils.StringUtils;
import com.swiot.biz.domain.Rule;
import com.swiot.biz.service.IRuleService;
import com.swiot.common.utils.poi.ExcelUtil;
import com.swiot.common.core.page.TableDataInfo;

/**
 * 规则业务Controller
 * 
 * @author kingdom
 * @date 2025-08-24
 */
@RestController
@RequestMapping("/biz/rule")
public class RuleController extends BaseController
{
    @Autowired
    private IRuleService ruleService;

    /**
     * 查询规则业务列表
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(Rule rule)
    {
        startPage();
        List<Rule> list = ruleService.selectRuleList(rule);
        return getDataTable(list);
    }

    /**
     * 导出规则业务列表
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:export')")
    @Log(title = "规则业务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rule rule)
    {
        List<Rule> list = ruleService.selectRuleList(rule);
        ExcelUtil<Rule> util = new ExcelUtil<Rule>(Rule.class);
        util.exportExcel(response, list, "规则业务数据");
    }

    /**
     * 获取规则业务详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(ruleService.selectRuleById(id));
    }

    /**
     * 新增规则业务
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:add')")
    @Log(title = "规则业务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rule rule)
    {
        // 参数检查
        if (StringUtils.isEmpty(rule.getRuleName())) {
            return error("规则名称不能为空");
        }
        
        // 检查规则名称是否已存在
        if (ruleService.checkRuleNameExists(rule.getRuleName())) {
            return error("规则名称已存在");
        }
        
        return toAjax(ruleService.insertRule(rule));
    }

    /**
     * 修改规则业务
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:edit')")
    @Log(title = "规则业务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rule rule)
    {
        // 参数检查
        if (StringUtils.isEmpty(rule.getId())) {
            return error("规则ID不能为空");
        }
        rule.setRuleName(null);

        
        return toAjax(ruleService.updateRule(rule));
    }


    /**
     * 删除规则业务
     */
    @PreAuthorize("@ss.hasPermi('biz:rule:remove')")
    @Log(title = "规则业务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ruleService.deleteRuleByIds(ids));
    }
}
